from django.db import models

from gis.common.django_ext.models import BaseModel
from itpay.core.agency.models import Account as AgencyAccount, ReceiptAddress, ReceiptConf
from itpay.core.mch.models import Account as MchAccount
from itpay.core.recharge_order.const import MatchMethod


class Order(BaseModel):
    agency = models.ForeignKey(AgencyAccount, on_delete=models.PROTECT, db_constraint=False)
    agency_name = models.CharField(max_length=50)
    mch = models.ForeignKey(MchAccount, on_delete=models.PROTECT, db_constraint=False)
    mch_name = models.CharField(max_length=50)
    user_id = models.CharField(max_length=50)  # 第三方用户id
    amount = models.BigIntegerField()  # 下单金额
    real_amount = models.BigIntegerField()  # 实际支付金额
    recharge_amount = models.BigIntegerField()  # 用户上分金额
    chased_amount = models.BigIntegerField(default=0)  # 已追回金额
    mch_recharge_id = models.CharField(max_length=100, null=True)  # 第三方订单id
    status = models.SmallIntegerField()  # 状态，
    source = models.SmallIntegerField()  # 自动创建或者im创建 see RechargeSourceType
    channel_order_no = models.CharField(max_length=50, null=True)  # 支付渠道订单号

    # 收款账号相关数据, 创建订单时从收款账户数据复制
    receipt_address = models.ForeignKey(ReceiptAddress, on_delete=models.PROTECT, null=True, db_constraint=False)
    receipt_conf = models.ForeignKey(ReceiptConf, on_delete=models.PROTECT, null=True, db_constraint=False)
    pay_type = models.IntegerField(null=True)  # PayType
    pay_method = models.IntegerField(null=True)  # PayMethodType
    receipt_type = models.IntegerField(null=True)  # ReceiptType
    receipt_method = models.IntegerField(null=True)  # ReceiptMethodType

    receipt_ext = models.CharField(max_length=1000, null=True)  # 收款账户信息
    pay_info_ext = models.CharField(max_length=1000, null=True)  # 付款账户信息

    # account = models.CharField(max_length=100, null=True)  # 账户（支付宝账号，微信账号，银行卡）
    # account_qr_code = models.CharField(max_length=100, null=True)  # 个人账号二维码（支付宝，微信）
    # nickname = models.CharField(max_length=50, null=True)  # 支付宝、微信个人昵称
    # ali_zkl = models.CharField(max_length=100, null=True)  # 支付宝吱口令
    # qr_code = models.CharField(max_length=100, null=True)  # 收款二维码
    # mch_name = models.CharField(max_length=50, null=True)  # 商户名称
    # huabei_fee_rate = models.IntegerField(null=True)  # 花呗收款手续费
    # credit_fee_rate = models.IntegerField(null=True)  # 信用卡收款手续费
    # first_name = models.CharField(max_length=20, null=True)  # 姓
    # last_name = models.CharField(max_length=20, null=True)  # 名
    # bank_code = models.CharField(max_length=10, null=True)  # 银行代码 BankCode
    # bank_branch = models.CharField(max_length=50, null=True)  # 银行支行

    phone = models.CharField(max_length=20, null=True)  # 银行卡绑定后机号
    auto_arrival = models.BooleanField(default=False)  # 自动上分
    can_comment = models.BooleanField(default=True)

    remark = models.CharField(max_length=200, null=True)

    success_at = models.DateTimeField(null=True)  # 充值成功时间
    chased_at = models.DateTimeField(null=True)  # 追分时间

    match_method = models.IntegerField(null=True, default=MatchMethod.HAND.value)  # see MatchMethod

    class Meta:
        unique_together = ('mch', 'mch_recharge_id')


class BankCardSms(BaseModel):
    """
    银行卡转账短信
    """
    out_id = models.IntegerField(unique=True)  # 来源id
    phone = models.CharField(max_length=20)  # 接收手机号
    from_phone = models.CharField(max_length=20)  # 发送手机号
    content = models.CharField(max_length=500)  # 短信内容
    amount = models.IntegerField(null=True)  # 转账金额
    recharge_order_id = models.IntegerField(null=True)  # 匹配成功的充值订单
    bank_code = models.CharField(null=True, max_length=20)  # 银行代码 BankCode
    recharge_account = models.CharField(max_length=50, null=True)  # 匹配成功的银行卡号
    sms_time = models.DateTimeField()  # 原始短信到达时间


class Comment(BaseModel):
    """
    订单评价
    """
    order = models.OneToOneField(Order, on_delete=models.PROTECT, db_constraint=False)
    agency = models.ForeignKey(AgencyAccount, on_delete=models.PROTECT, db_constraint=False)
    mch = models.ForeignKey(MchAccount, on_delete=models.PROTECT, db_constraint=False)
    user_id = models.CharField(max_length=50)  # 第三方用户id

    auto_comment = models.BooleanField(default=False)  # 自动评价

    amount = models.BigIntegerField()  # 充值金额
    real_amount = models.BigIntegerField()  # 充值金额
    recharge_amount = models.BigIntegerField(null=True)  # 用户上分金额
    success_at = models.DateTimeField(null=True)  # 充值成功时间

    pay_type = models.IntegerField(null=True)  # PayType
    receipt_type = models.IntegerField(null=True)  # ReceiptType

    comment_star = models.SmallIntegerField()  # 几星
    comment_rank = models.SmallIntegerField()  # 好中差
    comment_content = models.CharField(max_length=512, null=True)  # 评价内容
